package com.cw.jeeyt.service.impl.git;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cw.jeeyt.common.Order;
import com.cw.jeeyt.service.bo.git.GitUploadSqlRecordBo;
import com.cw.jeeyt.service.bo.security.SecurityUserBo;
import com.cw.jeeyt.core.util.SessionUtil;
import com.cw.jeeyt.dao.intf.git.GitUploadSqlRecordDao;
import com.cw.jeeyt.dao.model.git.GitUploadSqlRecordDo;
import com.cw.jeeyt.service.intf.git.GitUploadSqlRecordService;
import com.cw.jeeyt.service.converter.git.GitUploadSqlRecordConverter;
import com.cw.lang.mybatis.dto.PageInfo;

/**
 * 上传数据库脚本记录业务实现类
 *
 * @author chenw
 * @date 2019-02-25 01:04:34
 */
@Service
public class GitUploadSqlRecordServiceImpl implements GitUploadSqlRecordService {

    private static final Logger logger = LoggerFactory.getLogger(GitUploadSqlRecordServiceImpl.class);
    @Autowired
    private GitUploadSqlRecordDao gitUploadSqlRecordDao;//上传数据库脚本记录数据库操作接口

    /**
     * 查询所有上传数据库脚本记录
     */
    @Override
    public List<GitUploadSqlRecordBo> queryGitUploadSqlRecordList() throws Exception {
        return GitUploadSqlRecordConverter.doList2BoList(gitUploadSqlRecordDao.queryGitUploadSqlRecordList());
    }

    /**
     * 分页查询上传数据库脚本记录
     *
     * @param gitUploadSqlRecordBo
     * @param startNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    @Override
    public PageInfo<GitUploadSqlRecordBo> queryByPage(GitUploadSqlRecordBo gitUploadSqlRecordBo, Integer startNum,
        Integer pageSize, Order order) throws Exception {

        if (gitUploadSqlRecordBo == null) {
            logger.error("上传数据库脚本记录为空");
            return null;
        }
        PageInfo<GitUploadSqlRecordDo> pageInfo = gitUploadSqlRecordDao.queryByPage(
            GitUploadSqlRecordConverter.bo2Do(gitUploadSqlRecordBo), startNum, pageSize, order);
        return new PageInfo<>(pageInfo, GitUploadSqlRecordConverter.doList2BoList(pageInfo.getList()));
    }

    /**
     * 根据主键查询上传数据库脚本记录信息
     *
     * @return
     */
    @Override
    public GitUploadSqlRecordBo getGitUploadSqlRecordById(Long recordId) throws Exception {

        if (null == recordId) {
            logger.error("--主键recordId为空--");
            return null;
        }
        return GitUploadSqlRecordConverter.do2Bo(gitUploadSqlRecordDao.getById(recordId));
    }

    /**
     * 新增上传数据库脚本记录
     *
     * @param gitUploadSqlRecordBo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer insert(GitUploadSqlRecordBo gitUploadSqlRecordBo) throws Exception {

        if (gitUploadSqlRecordBo == null) {
            logger.error("上传数据库脚本记录为空");
            return null;
        }
        SecurityUserBo userBo = SessionUtil.getSessionUser();
        if (userBo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        GitUploadSqlRecordDo gitUploadSqlRecord = GitUploadSqlRecordConverter.bo2Do(gitUploadSqlRecordBo);
        gitUploadSqlRecord.setCreateBy(userBo.getId());
        gitUploadSqlRecord.setCreateDate(new Date());
        gitUploadSqlRecord.setUpdateBy(userBo.getId());
        gitUploadSqlRecord.setUpdateDate(new Date());
        Integer result = gitUploadSqlRecordDao.insert(gitUploadSqlRecord);
        return result;
    }

    /**
     * 新增上传数据库脚本记录不为空的字段
     *
     * @param gitUploadSqlRecordBo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer insertSelective(GitUploadSqlRecordBo gitUploadSqlRecordBo) throws Exception {

        if (gitUploadSqlRecordBo == null) {
            logger.error("上传数据库脚本记录为空");
            return null;
        }
        SecurityUserBo userBo = SessionUtil.getSessionUser();
        if (userBo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        GitUploadSqlRecordDo gitUploadSqlRecord = GitUploadSqlRecordConverter.bo2Do(gitUploadSqlRecordBo);
        gitUploadSqlRecord.setCreateBy(userBo.getId());
        gitUploadSqlRecord.setCreateDate(new Date());
        gitUploadSqlRecord.setUpdateBy(userBo.getId());
        gitUploadSqlRecord.setUpdateDate(new Date());
        Integer result = gitUploadSqlRecordDao.insertSelective(gitUploadSqlRecord);
        return result;
    }

    /**
     * 更新上传数据库脚本记录
     *
     * @param gitUploadSqlRecordBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(GitUploadSqlRecordBo gitUploadSqlRecordBo) throws Exception {

        if (gitUploadSqlRecordBo == null) {
            logger.error("上传数据库脚本记录为空");
            return;
        }
        SecurityUserBo userBo = SessionUtil.getSessionUser();
        if (userBo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        GitUploadSqlRecordDo gitUploadSqlRecord = GitUploadSqlRecordConverter.bo2Do(gitUploadSqlRecordBo);
        gitUploadSqlRecord.setUpdateBy(userBo.getId());
        gitUploadSqlRecord.setUpdateDate(new Date());

        gitUploadSqlRecordDao.update(gitUploadSqlRecord);
    }

    /**
     * 更新上传数据库脚本记录不为空的字段
     *
     * @param gitUploadSqlRecordBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateSelective(GitUploadSqlRecordBo gitUploadSqlRecordBo) throws Exception {

        if (gitUploadSqlRecordBo == null) {
            logger.error("上传数据库脚本记录为空");
            return;
        }
        SecurityUserBo userBo = SessionUtil.getSessionUser();
        if (userBo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        GitUploadSqlRecordDo gitUploadSqlRecord = GitUploadSqlRecordConverter.bo2Do(gitUploadSqlRecordBo);
        gitUploadSqlRecord.setUpdateBy(userBo.getId());
        gitUploadSqlRecord.setUpdateDate(new Date());

        gitUploadSqlRecordDao.updateSelective(gitUploadSqlRecord);
    }

    /**
     * 删除上传数据库脚本记录
     *
     * @param gitUploadSqlRecordBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(GitUploadSqlRecordBo gitUploadSqlRecordBo) throws Exception {

        if (gitUploadSqlRecordBo == null) {
            logger.error("上传数据库脚本记录为空");
            return;
        }
        GitUploadSqlRecordDo gitUploadSqlRecord = GitUploadSqlRecordConverter.bo2Do(gitUploadSqlRecordBo);
        gitUploadSqlRecordDao.delete(gitUploadSqlRecord);
    }
}
